package com.caowei.weicimpress.weicimpress;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.caowei.weicimpress.weicimpress.widget.MyListView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by caoweiPc on 2014/7/18.
 */
public class FileInfoAdapter extends BaseAdapter implements ActionBarBaseFragment.OnActivityCreatedListener {
    private static final String TAG=FileInfoAdapter.class.getSimpleName();
    private List<FileInfoBean> mData=new ArrayList<FileInfoBean>();
    private Context mContext;
    private Fragment mFragment;
    private Drawable selectedBg;
    public List<CheckBox> getCheckBoxList() {
        return checkBoxList;
    }

    private List<CheckBox> checkBoxList=new ArrayList<CheckBox>();

    public static class CheckBoxBoolean{
        int position;
        CheckBox checkBox;
        Boolean isChecked;
    };

    public Map<Integer, CheckBoxBoolean> getSelectedCheckboxes() {
        return selectedCheckboxes;
    }

    private Map<Integer,CheckBoxBoolean> selectedCheckboxes=new HashMap<Integer, CheckBoxBoolean>();
    public FileInfoAdapter(Fragment fragment){
        this.mContext=fragment.getActivity();
        this.mFragment=fragment;
    }

    @Override
    public void onActivityCreated() {
        MyListView listView= (MyListView) mFragment.getView().findViewById(R.id.list);
        selectedBg= mFragment.getActivity().getResources().getDrawable(R.drawable.frglistitemselectbg);
        listView.addCallback(new MyListView.CancleSelectedCheckbox() {
            @Override
            public void callBack() {
                //不整体刷新方式
                Map<Integer,CheckBoxBoolean> selectedCheckboxes2=
                        new HashMap<Integer, CheckBoxBoolean>(selectedCheckboxes);
                Set<Map.Entry<Integer,CheckBoxBoolean>> checkBoxSet= selectedCheckboxes2.entrySet();
                for(Map.Entry<Integer,CheckBoxBoolean> entry:checkBoxSet ){
                    CheckBoxBoolean checkBoxBoolean=entry.getValue();
                    checkBoxBoolean.checkBox.setTag(checkBoxBoolean.position);
                    checkBoxBoolean.checkBox.setChecked(true);
                    checkBoxBoolean.checkBox.performClick();

                }
                selectedCheckboxes.clear();

                /*界面整体刷新方式
                dataNotifySetChanged();
                LinearLayout footer= (LinearLayout) mFragment.getView().findViewById(R.id.footer2);
                if(footer.getVisibility()==View.VISIBLE){
                    footer.setVisibility(View.GONE);
                }*/
            }
        });
    }


    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public FileInfoBean getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return ((Object)getItem(position)).hashCode();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Log.d(TAG,"getView");
        ViewHolder viewHolder=null;
        if(convertView==null){
            View v= LayoutInflater.from(mContext).inflate(R.layout.filefragmentitem,parent,false);
            viewHolder=new ViewHolder(v,mFragment);
            v.setTag(viewHolder);
            convertView=v;
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        //解决重用ui组件导致的单选混乱
        final LinearLayout linearLayout=viewHolder.list_item;
        final CheckBox checkBox= viewHolder.checkbox;
        if(!checkBoxList.contains(checkBox)){
            checkBoxList.add(checkBox);
        }

        final LinearLayout footer= (LinearLayout) mFragment.getView().findViewById(R.id.footer2);

        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean  isChecked= checkBox.isChecked();
                if(isChecked){
                    CheckBoxBoolean checkBoxBoolean=new CheckBoxBoolean();
                    checkBoxBoolean.position=position;
                    checkBoxBoolean.checkBox=checkBox;
                    checkBoxBoolean.isChecked=isChecked;
                    selectedCheckboxes.put(checkBoxBoolean.position,checkBoxBoolean);
//                    linearLayout.setBackgroundResource(R.drawable.frglistitemselectbg);
                    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){
                        linearLayout.setBackground(selectedBg);
                    }else{
                        linearLayout.setBackgroundDrawable(selectedBg);
                    }
                    if(selectedCheckboxes.size()>0){
                        if(footer.getVisibility()==View.GONE){
                            footer.setVisibility(View.VISIBLE);
                        }
                    }
                }else{
                    if(Build.VERSION.SDK_INT< Build.VERSION_CODES.JELLY_BEAN){
                        linearLayout.setBackgroundDrawable(null);
                    }else{
                        linearLayout.setBackground(null);
                    }
                    Integer position1= (Integer) checkBox.getTag();
                    if(position1!=null){
                        selectedCheckboxes.remove(position1);
                    }else{
                        selectedCheckboxes.remove(position);
                    }
                    checkBox.setTag(null);
                    if(selectedCheckboxes.size()<=0){
                        if(footer.getVisibility()==View.VISIBLE){
                            footer.setVisibility(View.GONE);
                        }
                    }
                }

                if(mFragment instanceof EnableOrDisableBtnBySelectedCheckBoxes){
                    EnableOrDisableBtnBySelectedCheckBoxes temp= (EnableOrDisableBtnBySelectedCheckBoxes) mFragment;
                    temp.disableExtractBtn(selectedCheckboxes);
                 /*   if(!temp.disable(selectedCheckboxes)){
//                        temp.enable(selectedCheckboxes);
                    }*/
                }
                checkBox.setChecked(isChecked);
                checkBox.setFocusable(isChecked);
            }
        });

        boolean isSelected=(selectedCheckboxes.get(position)==null)||(
                ((CheckBoxBoolean)selectedCheckboxes.get(position)).isChecked==false);

        checkBox.setChecked(isSelected?false:true);
        checkBox.setFocusable(isSelected?false:true);

        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){
            if(!isSelected){
                linearLayout.setBackground(selectedBg);
            }else{
                linearLayout.setBackground(null);
            }
        }else{
            if(!isSelected){
                linearLayout.setBackgroundDrawable(selectedBg);
            }else {
                linearLayout.setBackgroundDrawable(null);
            }
        }
        //==============================
        FileInfoBean bean=getItem(position);
        viewHolder.fileName.setText(bean.getFileName());
        viewHolder.img.setImageResource(bean.getIcoId());

        StringBuilder sizeSb=new StringBuilder();
        if(bean.getSize().compareTo(BigDecimal.valueOf(-1))!=0){
            sizeSb.append(Utils.normalSizeStr(bean.getSize()));
        }else{
            sizeSb.append(mContext.getResources().getString(R.string.omit));
        }
        viewHolder.size.setText(sizeSb.toString());
        viewHolder.time_authrity.setText(bean.getDate_authrity());
        viewHolder.size.setVisibility(View.VISIBLE);
        viewHolder.checkbox.setVisibility(View.VISIBLE);
        viewHolder.setBean(bean);
        return convertView;
    }


    public void dataNotifySetChanged(){
        this.selectedCheckboxes.clear();
        this.notifyDataSetChanged();
    }

    public void setmData(List<FileInfoBean> data){
        this.mData=data;
    }

    public List<FileInfoBean> getmData(){
        return this.mData;
    }

    static class  ViewHolder{
        @InjectView(R.id.list_item)
        LinearLayout list_item;
        @InjectView(R.id.img)
        ImageView img;
        @InjectView(R.id.fileName)
        TextView fileName;
        @InjectView(R.id.time_authrity)
        TextView time_authrity;
        @InjectView(R.id.size)
        TextView size;
        @InjectView(R.id.checkbox)
        CheckBox checkbox;
        private FileInfoBean bean;
        private Fragment fragment;
        public ViewHolder(View v,Fragment fragment){
            this.fragment=fragment;
            ButterKnife.inject(this, v);
        }

        public FileInfoBean getBean() {
            return bean;
        }

        public void setBean(FileInfoBean bean) {
            this.bean = bean;
        }
    }

    public static interface EnableOrDisableBtnBySelectedCheckBoxes{
        public void disableExtractBtn(Map<Integer,CheckBoxBoolean> selected);
    }

}
